﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Dreamz
{
    public class UI
    {
        public const int ScreenWidth = 120;
        public const int ScreenHeight = 60;
        static public Coord CursorPos = new Coord(10, 32);
        static public Coord DescriptionPos = new Coord(10, 6);

        // Redraws the user interface
        static public void Draw()
        {
            CharMap descBorder = new CharMap(@"
################################################################################################################
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
#                                                                                                              #
################################################################################################################"
                , ConsoleColor.DarkBlue);
            ConsoleScreenDrawer.S.BufferDraw(descBorder, DescriptionPos.X - 6, DescriptionPos.Y - 5);

            if (!GameEngine.S.Player.CurrentRoom.IsVisited)
                ConsoleScreenDrawer.S.Draw(GameEngine.S.Player.CurrentRoom.FirstDescription, UI.DescriptionPos.X, UI.DescriptionPos.Y);
            else
                ConsoleScreenDrawer.S.Draw(GameEngine.S.Player.CurrentRoom.Description, UI.DescriptionPos.X, UI.DescriptionPos.Y);

            string exits = "";
            if (GameEngine.S.Player.CurrentRoom.North)
                exits += "[north] ";
            if (GameEngine.S.Player.CurrentRoom.East)
                exits += "[east] ";
            if (GameEngine.S.Player.CurrentRoom.South)
                exits += "[south] ";
            if (GameEngine.S.Player.CurrentRoom.West)
                exits += "[west] ";
            if (exits == "")
                ConsoleScreenDrawer.S.BufferDraw("[no open exits]", 10, descBorder.Height, ConsoleColor.Green);
            else
                ConsoleScreenDrawer.S.BufferDraw(exits, 10, descBorder.Height, ConsoleColor.Green);

            Console.DrawConsole();
            ConsoleScreenDrawer.S.BufferDraw(">", CursorPos.X - 2, CursorPos.Y);

            ConsoleScreenDrawer.S.Present();
            System.Console.SetCursorPosition(UI.CursorPos.X, UI.CursorPos.Y);
        }

        public class Console
        {
            static public CharMap CharMap = new CharMap(100, 20);
            static public Coord Position = new Coord(10, (short)(CursorPos.Y + 2));
            static int apa = 0;

            static Console()
            {
            }

            static public void DrawConsole()
            {
                ConsoleScreenDrawer.S.BufferDraw(CharMap, Position.X, Position.Y);
            }

            static public void Push(int lines)
            {
                for (int row = (CharMap.Height - 1) - lines; row >= 0; row--)
                {
                    for (int col = 0; col < CharMap.Width; col++)
                    {
                        CharMap.Chars[col, row + lines] = CharMap.Chars[col, row];
                    }
                }

                for (int row = 0; row < lines; row++)
                {
                    for (int col = 0; col < CharMap.Width; col++)
                    {
                        CharMap.Chars[col, row].Char.AsciiChar = (byte)0;
                    }
                }
            }

            static void Fill(char value)
            {
                for (int row = 0; row < CharMap.Chars.GetLength(1); row++)
                {
                    for (int col = 0; col < CharMap.Chars.GetLength(0); col++)
                    {
                        CharMap.Chars[col, row].Char.AsciiChar = (byte)value;
                    }
                }
            }

            static public void Clear()
            {
                CharMap = new CharMap(CharMap.Width, CharMap.Height);
            }

            static public void Write(string value)
            {
                short attributes = (short)ConsoleColor.Gray;

                string[] split = value.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);

                Push(split.Length + 1);

                for (int col = 0; col < CharMap.Width; col++)
                {
                    CharMap.Chars[col, 0].Char.AsciiChar = (byte)' ';
                }

                for (int row = 0; row < split.Length && row < CharMap.Height; row++)
                {
                    for (int col = 0; col < split[row].Length && col < CharMap.Width; col++)
                    {
                        CharMap.Chars[col, row].Char.AsciiChar = (byte)split[row][col];
                        CharMap.Chars[col, row].Attributes = attributes;
                    }
                }
            }
            static public void Write(string value, ConsoleColor Color)
            {
                short attributes = (short)Color;

                string[] split = value.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);

                Push(split.Length + 1);

                for (int row = 0; row < split.Length && row < CharMap.Height; row++)
                {
                    for (int col = 0; col < split[row].Length && col < CharMap.Width; col++)
                    {
                        CharMap.Chars[col, row].Char.AsciiChar = (byte)split[row][col];
                        CharMap.Chars[col, row].Attributes = attributes;
                    }
                }
            }
            static public void Write(string format, params object[] args)
            {
                Write(string.Format(format, args));
            }
            static public void Write(ConsoleColor Color, string format, params object[] args)
            {
                Write(string.Format(format, args), Color);
            }

            // Here WriteLine() does exactly the same as Write(). It's supported for compatibility with System.Console
            static public void WriteLine(string value)
            {
                Write(value);
            }
            static public void WriteLine(string value, ConsoleColor Color)
            {
                Write(value, Color);
            }
            static public void WriteLine(string format, params object[] args)
            {
                Write(format, args);
            }
            static public void WriteLine(string format, ConsoleColor Color, params object[] args)
            {
                Write(Color, format, args);
            }
        }
    }
}
